/**
 * @author loaner
 */
class com.drunkencop.FlashCacher {

	private var _xml : XML;

	private var _currentfolder : XMLNode;

	private var _currentfile : XMLNode;

	private var _soundloader : Sound;

	private var _moviecliploader : MovieClipLoader;

	private var _mcl_listener : Object;

	private var _callback : Function;

	private var _totalbytesloaded : Number;

	private var _totalbytesneeded : Number;

	private var _currentfilesize : Number;

	private var _currentfilebytesloaded : Object;

	private var _currentfolderbytesloaded : Object;

	private var _emptyclip : MovieClip;

	private var _donecallback : Function;
	
	public function setCallback( funcToCall : Function) : Void 
	{
		_callback = funcToCall;
	}

	public function load( filename : String) : Void 
	{
		initLoaders();
		_xml = new XML();
		_xml.ignoreWhite = true;
		_xml.onLoad = Delegate.create( this, onXMLLoaded );
		_xml.load( filename );
		
	}
	
	private function initLoaders()
	{
		_soundloader = new Sound();
		_moviecliploader = new MovieClipLoader();
		_mcl_listener = new Object();
		_mcl_listener.onLoadInit = Delegate.create( this, doneLoadingFile );
//		_mcl_listener.onLoadProgress = Delegate.create( this, onLoadProgressHandler );
		_moviecliploader.addListener( _mcl_listener );
		var _emptyclipcontainer:MovieClip = _root.createEmptyMovieClip(" _emptyclip", _root.getNextHighestDepth() );
		_emptyclip = _emptyclipcontainer.createEmptyMovieClip(" _emptyclip", _emptyclipcontainer.getNextHighestDepth() );
		_emptyclipcontainer._visible = false;
	}

	private function onXMLLoaded() : Void 
	{
		trace("Loaded _xml " + _xml );
		var x:XMLNode = _xml.firstChild;
		trace("x.childNodes.length = " + x.childNodes.length);
		_totalbytesloaded = 0;
		_totalbytesneeded = Number( x.attributes.size );	
		_currentfolder = x.childNodes[0];
		_currentfile = _currentfolder.childNodes[0];
		loadFolder();
	}
	
	private function loadFolder( )
	{
		trace("loadFolder, and _currentfolder.childNodes.length = " + _currentfolder.childNodes.length );
		_currentfolderbytesloaded = 0;
		_currentfile = _currentfolder.firstChild;
		loadFile();
	}
	
	private function loadFile()
	{
		var filename : String = _currentfile.attributes.name;
		_currentfilesize = Number( _currentfile.attributes.size );
		if( filename.indexOf( ".mp3" ) > -1 )
		{
			_soundloader.onLoad = Delegate.create( this, doneLoadingFile );
			_soundloader.loadSound( _currentfile.attributes.name );
		}
		else
		{
			_moviecliploader.loadClip( _currentfile.attributes.name, _emptyclip );
		}
		useCallback();
	}
	
	private function doneLoadingFile( success:Boolean)
	{
		useCallback();
		trace("doneLoadingFile and _currentfile.nextSibling = " + _currentfile.nextSibling);
		if( _currentfile.nextSibling == null )
		{
			doneLoadingFolder();	
		}
		else
		{
			_currentfile = _currentfile.nextSibling;
			loadFile();
		}
		
		_totalbytesloaded += _currentfilesize;
		_currentfolderbytesloaded += _currentfilesize;
		useCallback();
	}
	
	private function onLoadProgressHandler( targetclip:MovieClip, bytesloaded:Number, bytestotal:Number)
	{
		var progress:Object = _moviecliploader.getProgress( _emptyclip );
		_currentfilebytesloaded = progress.bytesLoaded;
		
		useCallback();
	}
	
	private function useCallback()
	{
		var info:Object = { 
					currentfile:_currentfile.attributes.name, 
					currentfolder:_currentfolder.attributes.name,
					filebytesloaded:_currentfilebytesloaded,
					filebytesneeded: _currentfile.attributes.size,
					folderbytesloaded: _currentfolderbytesloaded,
					folderbytesneeded: _currentfolder.attributes.size,
					totalbytesloaded: _totalbytesloaded,
					totalbytesneeded: _totalbytesneeded
		};
		_callback( info );
	}
	
	private function doneLoadingFolder()
	{
		if( _currentfolder.nextSibling == null )
		{
			doneLoadingEverything();	
		}
		else
		{
			_currentfolder = _currentfolder.nextSibling;
			loadFolder();
		}
		useCallback();
	}
	
	private function doneLoadingEverything()
	{
		trace("DONE LOADING EVERYTHING");	
		_donecallback();
	}

	public function setDoneCallback(arg0 : Function) : Void 
	{
		_donecallback = arg0;	
	}

	

}